/**
 * 
 */
package com.wbcs.system.function.ifexpression;

import com.wbcs.exception.WbcsConfigLoadingException;
import com.wbcs.exception.WbcsRuntimeException;
import com.wbcs.system.function.AbsFunc;
import com.wbcs.system.function.FuncContextBean;
import com.wbcs.system.function.IFuncDo;

/**
 * @author hawkfly
 */
public abstract class AbsIfFunc extends AbsFunc {

    String lstNameUpChain;
    String do1;
    String do2;
    FuncContextBean context;

    public AbsIfFunc(String funcName, String lstNameUpChain, String do1, String do2){
        super(do1, do2);

        this.funcName = funcName;
        this.lstNameUpChain = lstNameUpChain;
        this.do1 = do1;
        this.do2 = do2;
    }

	public static Object createFunctionObj(Class funclass, String funName, String affector, String doTrue, String doFalse)
	{
		if(funclass==null) return null;
		
        try
        {
        	return funclass.getConstructor(
                    new Class[] { String.class, String.class, String.class, String.class }).newInstance(
                    new Object[] { funName, affector, doTrue, doFalse });
        	
        }
        catch(Exception e)
        {
            throw new WbcsConfigLoadingException("创建函数"+funclass.getName()+"对象失败",e);
        }
		
	}

    @Override
    public boolean exec(IFuncDo ido) {
        context = ido.getFuncContext(lstNameUpChain);
        Object upChainParamValObj = context.getUpChainPara(lstNameUpChain);
        if(upChainParamValObj == null)
        {
            throw new WbcsRuntimeException("chain类型报表不能获取forwardParam参数设置到Request中的有效值");
        }
        return exec(upChainParamValObj, ido);
    }

    /**
     * 具体函数执行逻辑
     * @param ifObj 逻辑影响因素
     * @return 执行是否成功
     */
    abstract boolean exec(Object ifObj, IFuncDo ido);
}
